/*
 * @Author: lvye
 * @Description:
 * @Date: 2023-06-29 16:50:58
 * @LastEditTime: 2023-09-07 10:00:18
 * @FilePath: \vue3-ts\src\store\index.ts
 */
import { Store, createStore, useStore as useVuexStore } from "vuex"
import { IRootState, IStoreType } from "./type"
import login from "./login/login"
import system from "./system/system"
import { treeData } from "@/mock/tree"
// import { getPageListData } from '@/service/main/system/system'
const store = createStore<IRootState>({
  state: () => {
    return {
      name: "coderwhy",
      age: 18,
      entireDepartment: [],
      entireRole: [],
      entireMenu: []
    }
  },
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list
    },
    changeEntireRole(state, list) {
      state.entireRole = list
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list
    }
  },
  getters: {},
  actions: {
    // 请求部门和角色数据
    async getInitialDataAction({ commit }) {
      // const departmentResult = await getPageListData("/department/list", {
      //   offset: 0,
      //   size: 1000
      // })
      // const { list: departmentList } = departmentResult.data
      // const roleResult = await getPageListData("/role/list", {
      //   offset: 0,
      //   size: 1000
      // })
      // const { list: roleList } = roleResult.data
      // const menuResult = await getPageListData("/menu/list", {})
      // const { list: menuList } = menuResult.data
      // 接口不同 mock数据
      const departmentList = [
        { name: "技术部", id: 1 },
        { name: "产品部", id: 2 },
        { name: "市场部", id: 3 },
        { name: "运营部", id: 4 }
      ]
      // 角色列表
      const roleList = [
        { name: "超级管理员", id: 1 },
        { name: "管理员", id: 2 },
        { name: "编辑员", id: 3 },
        { name: "运营员", id: 4 }
      ]
      const menuList: any[] = treeData
      // 2.保存数据
      commit("changeEntireDepartment", departmentList)
      commit("changeEntireRole", roleList)
      commit("changeEntireMenu", menuList)
    }
  },
  modules: {
    login,
    system
  }
})
export function setupStore() {
  store.dispatch("login/loadLocalLogin")
  // store.dispatch("getInitialDataAction")
}
export function useStore(): Store<IStoreType> {
  return useVuexStore()
}
export default store
